package com.example.srm.dict.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.srm.auth.anno.Authority;
import com.example.srm.common.controller.BaseController;
import com.example.srm.constant.DataDictionaryConstant;
import com.example.srm.dict.service.TDataDictionaryService;
import com.example.srm.domain.TDataDictionary;
import com.example.srm.domain.vo.PageView;
import com.example.srm.domain.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author pfk
 * @creatTime 2021/11/29 09:17
 * @describe    类型明细
 */
@RestController
@RequestMapping("/dict_detail")
@CrossOrigin
public class DataDictionaryController extends BaseController<TDataDictionaryService,TDataDictionary> {

    @Autowired
    private TDataDictionaryService service;


    /**
     * 查询
     */
    @RequestMapping(value = "/query1",method = RequestMethod.GET)
    public Result query(Integer page, Integer pageSize,String typeId){
        //校验参数是否合法
        if (page==null||page<0){
            page=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=10;
        }

        Page<TDataDictionary> tPage;
        if (typeId!=null){
            tPage = service.page(new Page<>(page,pageSize),new QueryWrapper<TDataDictionary>().eq(DataDictionaryConstant.typeId,typeId));
        }else {
            tPage = service.page(new Page<>(page,pageSize));
        }
        PageView<TDataDictionary> pageView = new PageView<TDataDictionary>()
                .setRows(tPage.getRecords())
                .setTotalCount(tPage.getTotal())
                .setTotalPage(new Integer(tPage.getPages()+""));

        return new Result().setData(pageView).setSuccess(true);
    }

    @Override
    @Authority(auth = {"system:dictDetail:add"})
    public Result save(@RequestBody TDataDictionary tDataDictionary, HttpServletRequest request) throws Exception {
        return super.save(tDataDictionary, request);
    }

    @Override
    @Authority(auth = {"system:dictDetail:update"})
    public Result update(@RequestBody TDataDictionary tDataDictionary, HttpServletRequest request) throws Exception {
        return super.update(tDataDictionary, request);
    }

    @Override
    @Authority(auth = {"system:dictDetail:delete"})
    public Result delete(String id) {
        return super.delete(id);
    }

    /**
     * 更新前检查参数
     * @param tDataDictionary
     */
    @Override
    public void beforeUpdate(TDataDictionary tDataDictionary) {
        //判断修改的角色是否存在
        TDataDictionary type = service.getOne(new QueryWrapper<TDataDictionary>().eq(DataDictionaryConstant.id, tDataDictionary.getId()));
        if (type==null) {
            throw  new RuntimeException("类别明细"+tDataDictionary.getName()+"不存在");
        }
        TDataDictionary nameDict = service.getOne(new QueryWrapper<TDataDictionary>().eq(DataDictionaryConstant.name, tDataDictionary.getName()));
        if (nameDict!=null) {
            throw  new RuntimeException("类别明细名为"+tDataDictionary.getName()+"已经存在");
        }
    }

    /**
     * 保存前检查参数
     * @param tDataDictionary
     */
    @Override
    public void beforeSave(TDataDictionary tDataDictionary) throws Exception {
        TDataDictionary dict = service.getOne(new QueryWrapper<TDataDictionary>().eq(DataDictionaryConstant.name,tDataDictionary.getName()));
        if (dict!=null) {
            throw new RuntimeException("类别明细名称已经存在");
        }
    }

}
